package com.young.ysepay.service.impl;

import com.alibaba.fastjson.JSON;
import com.young.ysepay.dto.YsPayDTO;
import com.young.ysepay.service.YsBuildRequestParaService;
import com.young.ysepay.service.YsBusinessPramService;
import com.young.ysepay.service.YsDownloadBillService;
import com.young.ysepay.service.YsVerifyJsonSignService;
import com.young.ysepay.utils.DateFormatHelper;
import com.young.ysepay.utils.Https;
import com.young.ysepay.utils.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * describe:
 *
 * @author zhaopengjun
 * @date 2019/8/7 21:18
 */
@Service
public class YsDownloadBillServiceImpl implements YsDownloadBillService {
    @Autowired
    private YsBusinessPramService ysBusinessPramService;
    @Autowired
    private YsBuildRequestParaService ysBuildRequestParaService;
    @Autowired
    YsVerifyJsonSignService ysVerifyJsonSignService;
    private static Logger log = LoggerFactory.getLogger(YsDownloadBillServiceImpl.class);
    @Override
    public String getBill(String accountDate) {
        YsPayDTO ysPayDTO = ysBusinessPramService.ysBusinessPram();
        Map<String, String> resultMap = new HashMap<>();
        Map<String, String> paramsMap = new HashMap<>();
        //封装公共参数以及业务参数
        Map<String, String> bizContent = new HashMap<>();
        bizContent.put("account_date",accountDate);
        //加急
        paramsMap.put("method", "ysepay.online.bill.downloadurl.get");
        paramsMap.put("partner_id", ysPayDTO.getPARTNER_ID());
        paramsMap.put("timestamp", DateFormatHelper.getNowTimeStr());
        paramsMap.put("charset", ysPayDTO.getDEFAULT_CHARSET());
        paramsMap.put("sign_type", ysPayDTO.getSIGN_ALGORITHM());
        paramsMap.put("version", ysPayDTO.getVERSION());
        paramsMap.put("notify_url", ysPayDTO.getNOTIFY_TRANSFER_URL());
        paramsMap.put("biz_content",  JSON.toJSONString(bizContent));
        String str = "";
        try {
            Map<String, String> sParaMap = ysBuildRequestParaService.buildRequestPara(paramsMap);
            String returns = Https.httpsSend(ysPayDTO.getYSEPAY_GATEWAY_URL(), sParaMap);
            resultMap = (Map) JSON.parse(returns);
            log.error("调用银盛分账退款返回值"+resultMap);
            Object obj = resultMap.get("ysepay_online_bill_downloadurl_get_response");
            boolean verifyResult = false;
            if(obj != null){
                int yscount = returns.toString().indexOf("ysepay_online_bill_downloadurl_get_response");
                str = returns.substring(yscount);
                str = str.replace("ysepay_online_bill_downloadurl_get_response", "");
                str = str.substring(2, str.length() - 1);
                verifyResult = ysVerifyJsonSignService.verifyJsonSign(resultMap.get("sign").toString(), str, "utf-8");
                log.info("YsDownloadBillServiceImpl -------------------routingJointVerifyParam"+"校验是否成功"+verifyResult+str);
                return str;
            }
    }catch (Exception ee){
            ee.printStackTrace();
        }finally {
            return str;
        }
    }
}
